package com.wdb.common.configurer.authority;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import java.util.Base64;
import java.util.Date;
import java.util.Map;

public class AuthorityExecutor {
    private String secret;

    public AuthorityExecutor(String secret) {
        this.secret = secret;
    }

    public String compact(Map<String, Object> claims, Date iat, Date exp) {
        return Jwts.builder()
                .setIssuedAt(iat)
                .addClaims(claims)
                .setExpiration(exp)
                .signWith(SignatureAlgorithm.HS512, Base64.getEncoder().encodeToString(secret.getBytes()))
                .compact();
    }

    public Jws<Claims> parse(String jwt) {
        return Jwts.parser()
                .setSigningKey(Base64.getEncoder().encodeToString(secret.getBytes()))
                .parseClaimsJws(jwt);
    }
}
